<?php
/**
 * created by zzy
 * date: 2017/11/20 10:11
 */


namespace App\Eloquent\Ygt;


class ProcessProcess extends DbEloquent
{
    protected $table = 'ygt_process_process';
    public static $orderby_id   = false;

    //设置 工序无限极
    public static function setProcessData( $lists ){
        $listsArr                  = $processIdArr = [];
        foreach( $lists as $key=>$val ){
            $listsArr[$val['id']]  = $val;
            $processIdArr[]       = $val['id'];
            $lists[$key]['sids']     = [];
        }
        $processProcessLists      = self::getList( ['process_id'=>['in',$processIdArr]] );
        foreach( $processProcessLists as $key=>$val ){
            $listsArr[$val['process_id']]['sids'][]   = $val['son_process_id'];
        }
        foreach( $lists as $key=>$val ){
            if( isset($listsArr[$val['id']]['sids']) ){
                $lists[$key]['sids']     = $listsArr[$val['id']]['sids'];
            } else {
                $lists[$key]['sids']     = [];
            }
        }
//        foreach( $listsArr as $key=>$val ){
//            print_r($val);
//            echo '<hr/>';
//        }exit;
//        print_r($listsArr);exit;
        $loop                       = 0;
        foreach( $lists as $key=>$val ){
            $dataIds               = $val['sids'];
            $data                   = $allSids = [];
            $allSids[]             = $val['id'];
            if( $dataIds ) {
                $do                 = true;
                while( $do ){
                    $do             = false;
                    foreach( $dataIds as $k=>$v ){
                        $is_kong            = empty($listsArr[$v]['sids']);
                        $son_id             = array_shift( $dataIds );
                        $allSids[]         = $son_id;
                        if( $is_kong ){
                            if( isset($listsArr[$v]) ){
                                $data[]     = $listsArr[$v];
                            }
                        } else {
                            $do             = true;
                            $newIds        = $listsArr[$v]['sids'];
                            $dataIds       = array_merge( $dataIds, $newIds );
                        }
                    }
//                    if($val['id'] == 16){
//                        //var_dump($dataIds);echo '<hr/>';
//                    }
                    $loop++;
                    if($loop > 1000){
                        exit('死循环了');
                    }
                }
            }
            $lists[$key]['data']        = $data;
            $lists[$key]['all_sids']    = $allSids;
        }
        //exit;
        return $lists;
    }
}
